package com.jifeng.api.crud.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jifeng.api.crud.constant.FieldConstant;
import com.jifeng.api.crud.dao.RentDao;
import com.jifeng.api.crud.entity.HeartEntity;
import com.jifeng.api.crud.entity.RentEntity;
import com.jifeng.api.crud.service.RentService;
import com.jifeng.api.crud.utils.CheckParamsRs;
import com.jifeng.api.utils.HttpUtils;
import com.jifeng.api.utils.PageUtils;
import com.jifeng.api.utils.Query;
import com.jifeng.api.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.Objects;


@Service("rentService")
@Slf4j
public class RentServiceImpl extends ServiceImpl<RentDao, RentEntity> implements RentService {

    @Autowired
    CheckParamsRs checkParamsRs;

    public String getRequestUrl() {
        return ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest().getRequestURI();
    }

    public HttpServletRequest getRequest() {
        return ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<RentEntity> page = this.page(
                new Query<RentEntity>().getPage(params),
                new QueryWrapper<RentEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public R optCabitControl(Map<String, Object> params) {
        try {
            Object imei = params.get("imei");
            Object num = params.get("num");
//            RentEntity rentEntity = this.getOne(new QueryWrapper<RentEntity>().eq("IOT", imei));
//            if (rentEntity != null) {
//                log.error("path：{}，errMsg：{}", "仓门控制", "设备已租赁");
//                return R.error(400, "设备已租赁");
//            }
            boolean openRs = HttpUtils.optCabitControl(imei.toString(), num.toString());
            if (openRs) {
                return R.ok();
            }
            return R.error();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("path：{}，errMsg：{}", "仓门控制", e.getMessage());
            return R.sysError();
        }

    }

    @Override
    public R optCabitStauts(Map<String, Object> params) {
        try {
            Object imei = params.get("imei");
            Object status = params.get("status");
            boolean openRs = HttpUtils.optCabitStauts(imei.toString(), status.toString());
            if (openRs) {
                return R.ok();
            }
            return R.error();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("path：{}，errMsg：{}", "仓门禁用状态控制", e.getMessage());
            return R.sysError();
        }

    }

    @Override
    public R add(RentEntity rent) {
        try {
            /**
             * 校验编号格式
             */
            boolean checkIotRs = checkParamsRs.checkIot(rent.getIot());
            if (!checkIotRs) {
                log.warn("path：{}，errMsg：{}", getRequestUrl(), "iot is wrong");
                return R.error(404, "iot is wrong");
            }

            /**
             * 校验是否存在
             */
            RentEntity heart = this.getOne(new QueryWrapper<RentEntity>().eq(FieldConstant.T_TERMINAL_IOT, rent.getIot()), false);
            if (heart != null) {
                log.warn("path：{}，errMsg：{}", getRequestUrl(), "heart has exist");
                return R.error(400, "heart has exist");
            }

            boolean save = this.save(rent);
            if (save) {
                return R.ok();
            }
            return R.error();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("path：{}，errMsg：{}", "增加租赁信息", e.getMessage());
            return R.sysError();
        }

    }

    @Override
    public R myUpdate(RentEntity rent) {
        try {
            /**
             * 校验编号格式
             */
            boolean checkIotRs = checkParamsRs.checkIot(rent.getIot());
            if (!checkIotRs) {
                log.warn("path：{}，errMsg：{}", getRequestUrl(), "iot is wrong");
                return R.error(404, "iot is wrong");
            }

            /**
             * 校验是否存在
             */
            RentEntity heart = this.getOne(new QueryWrapper<RentEntity>().eq(FieldConstant.T_TERMINAL_IOT, rent.getIot()), false);
            if (heart == null) {
                log.warn("path：{}，errMsg：{}", getRequestUrl(), "heart no exist");
                return R.error(400, "heart no exist");
            }

            boolean update = this.update(rent,new UpdateWrapper<RentEntity>().eq("IOT",rent.getIot()));
            if (update) {
                return R.ok();
            }
            return R.error();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("path：{}，errMsg：{}", "更新租赁信息", e.getMessage());
            return R.sysError();
        }
    }

}